﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Forms.Skin
{
    internal class FormConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
        {
            if (destinationType == typeof(FormButton))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
        {
            if (destinationType == typeof(System.String) && value is FormButton)
            {
                FormButton so = (FormButton)value;
                return "(Image)";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    string s = (string)value;
                    int colon = s.IndexOf(':');
                    int comma = s.IndexOf(',');
                    if (colon != -1 && comma != -1)
                    {
                        string checkWhileTyping = s.Substring(colon + 1, (comma - colon - 1));
                        colon = s.IndexOf(':', comma + 1);
                        comma = s.IndexOf(',', comma + 1);
                        string checkCaps = s.Substring(colon + 1, (comma - colon - 1));
                        colon = s.IndexOf(':', comma + 1);
                        string suggCorr = s.Substring(colon + 1);
                        // Dim so As New FormControlBox
                        // so.SpellCheckWhileTyping = [Boolean].Parse(checkWhileTyping)
                        // so.SpellCheckCAPS = [Boolean].Parse(checkCaps)
                        // so.SuggestCorrections = [Boolean].Parse(suggCorr)
                        return "123";
                    }
                }
                catch
                {
                    throw new ArgumentException("无法将\" & DirectCast(value, String) & \"转换为 SpellingOptions 类型");
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

}
